package com.guigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;

import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.ssyx.acl.mapper.AdminRoleMapper;
import com.guigu.ssyx.acl.mapper.RoleMapper;
import com.guigu.ssyx.acl.service.AdminRoleService;
import com.guigu.ssyx.acl.service.AdminService;
import com.guigu.ssyx.acl.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    AdminRoleMapper adminRoleMapper;

    @Autowired
    AdminRoleService adminRoleService;

    @Override
    public IPage<Role> selectPage(Page<Role> rolePage, RoleQueryVo roleQueryVo) {
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            roleLambdaQueryWrapper.like(Role::getRoleName, roleName);
        }
        IPage<Role> pageModel = roleMapper.selectPage(rolePage, roleLambdaQueryWrapper);
        return pageModel;
    }

    @Override
    public Map<String, Object> getRoleByAadminId(Long adminId) {
        //1.查询所有角色
        List<Role> roleList = roleMapper.selectList(null);

        //2.根据用户id查询已分配角色
        List<Role> assignRoleList = adminRoleMapper.selectRoleByAdminId(adminId);


        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("allRolesList", roleList);
        roleMap.put("assignRoles", assignRoleList);
        return roleMap;
    }

    /**
     * 分配角色
     *
     * @param adminId
     * @param roleId
     */
    @Override
    public void saveUserRoleRealtionShip(Long adminId, Long[] roleId) {
        //删除用户角色关系
        LambdaUpdateWrapper<AdminRole> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleMapper.delete(updateWrapper);

        // 分配新角色
        if (roleId != null && roleId.length > 0) {
            List<AdminRole> adminRoles = new ArrayList<>();
            for (Long rid : roleId) {
                if (rid == null) continue;
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(adminId);
                adminRole.setRoleId(rid);
                adminRoles.add(adminRole);
            }
            if (!adminRoles.isEmpty()) {
                adminRoleService.saveBatch(adminRoles);
            }
        }
    }
}